package util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.dom4j.*;
import org.dom4j.io.SAXReader;

public class XmlUtil {

	private SAXReader reader;
	private Document document;
	private File xmlFile;

	public XmlUtil(String fileName) throws Exception {
		xmlFile = new File(fileName);
		reader = new SAXReader();
		document = reader.read(xmlFile);
	}

	public Element getElementById(String nodeName, int id) {
		return (Element) document.selectSingleNode((new StringBuilder("//"))
				.append(nodeName).append("[@id=").append(id).append("]")
				.toString());
	}

	public Element getElementById(int id) {
		return (Element) document.selectSingleNode((new StringBuilder(
				"//*[@id=")).append(id).append("]").toString());
	}

	public String getNodeText(Element e, String nodeName) {
		String txt = "";
		Element e1 = e.element(nodeName);
		if (e1 != null) {
			txt = e1.getText();
		}
		return txt;
	}

	public List readMultiNodes(String nodeString) {
		List nodes = new ArrayList();
		return nodes;
	}

	public String readSingleNodes(String nodeString) {
		Node node = document.selectSingleNode(nodeString);
		return node.getText();
	}

	public Document getDocument() {
		return document;
	}
}
